package com.info.zhiduoduo.admin.service;

import com.info.zhiduoduo.admin.model.formbean.crowdsource.CityConfigFormBean;
import com.info.zhiduoduo.admin.model.formbean.crowdsource.CityConfigQuotaFormBean;
import com.info.zhiduoduo.admin.model.formbean.crowdsource.CompanyQuotaFormBean;
import com.info.zhiduoduo.admin.model.formbean.crowdsource.MakerQuotaFormBean;
import com.info.zhiduoduo.admin.model.formbean.crowdsource.listCityConfigsFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.repository.dto.quota.CityConfigDto;
import com.info.zhiduoduo.repository.dto.quota.CompanyQuotaDto;
import com.info.zhiduoduo.repository.dto.quota.CrowdsourceCityQuotaDto;
import com.info.zhiduoduo.repository.dto.quota.MakerQuotaDto;
import com.info.zhiduoduo.repository.dto.quota.ZhiduoduoCityQuotaDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.maker.MakerStudio;
import com.info.zhiduoduo.repository.entity.quota.CityConfig;
import com.info.zhiduoduo.repository.entity.quota.CompanyQuota;
import com.info.zhiduoduo.repository.entity.quota.CrowdsourceCityQuota;
import com.info.zhiduoduo.repository.entity.quota.MakerQuota;
import com.info.zhiduoduo.repository.mapper.quota.CityConfigMapper;
import com.info.zhiduoduo.repository.mapper.quota.CompanyQuotaMapper;
import com.info.zhiduoduo.repository.mapper.quota.CrowdsourceCityQuotaMapper;
import com.info.zhiduoduo.repository.mapper.quota.MakerQuotaMapper;
import com.info.zhiduoduo.repository.mapper.quota.ZhiduoduoCityQuotaMapper;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class QuotaService {

	@Autowired
	MakerService makerService;

	@Resource
	CityConfigMapper cityConfigMapper;
	@Resource
	CrowdsourceCityQuotaMapper crowdsourceCityQuotaMapper;
	@Resource
	ZhiduoduoCityQuotaMapper zhiduoduoCityQuotaMapper;
	@Resource
	MakerQuotaMapper makerQuotaMapper;
	@Resource
	CompanyQuotaMapper companyQuotaMapper;

	public SmartResult addCityConfig(BaseSimpleFormBean<CityConfigFormBean> formBean) {
		CityConfigFormBean reqdata = formBean.getReqdata();
		CityConfig cityConfig = new CityConfig().setId(reqdata.getId())
				.setProvinceId(reqdata.getProvinceId())
				.setCityId(reqdata.getCityId())
				.setName(reqdata.getName())
				.setRemark(reqdata.getRemark())
				.setPartyId(reqdata.getPartyId());
		if (cityConfig.insertOrUpdate()) {
			return SmartResult.success(cityConfig.getId());
		}
		return SmartResult.fail();
	}

	public SmartResult<CityConfigDto> listCityConfigs(BaseSimpleFormBean<listCityConfigsFormBean> formBean) {
		@Valid listCityConfigsFormBean reqdata = formBean.getReqdata();
		String name = reqdata.getName();
		List<CityConfigDto> list = cityConfigMapper.findList(name);
		for (CityConfigDto configDto : list) {
			configDto.setCrowdsourceCityQuotaList(crowdsourceCityQuotaMapper.listByConfigId(configDto.getId()));
			configDto.setZhiduoduoCityQuota(zhiduoduoCityQuotaMapper.getByConfigId(configDto.getId()));
		}
		return SmartResult.success(list);
	}

	public SmartResult editCityConfigQuota(BaseSimpleFormBean<CityConfigQuotaFormBean> formBean) {
		@Valid CityConfigQuotaFormBean reqdata = formBean.getReqdata();
		int id = reqdata.getId();
		CityConfig cityConfig = new CityConfig().selectById(id);
		if (cityConfig == null) {
			return SmartResult.fail(StatusMsg.StatusMsg_102);
		}
		BeanUtils.copyProperties(reqdata, cityConfig);
		boolean b = cityConfig.setUpdatetime(new Date()).updateById();
		if (!b) {
			return SmartResult.fail(StatusMsg.StatusMsg_199);
		}
		// 智众包城市限额
		List<CrowdsourceCityQuotaDto> crowdsourceCityQuotaList = reqdata.getCrowdsourceCityQuotaList();
		if (CollectionUtils.isNotEmpty(crowdsourceCityQuotaList)) {
			for (CrowdsourceCityQuotaDto crowdsourceCityQuotaDto : crowdsourceCityQuotaList) {
				CrowdsourceCityQuota crowdsourceCityQuota = new CrowdsourceCityQuota();
				BeanUtils.copyProperties(crowdsourceCityQuotaDto, crowdsourceCityQuota);
				crowdsourceCityQuota.insertOrUpdate();
			}
		}
		//  城市限额
		ZhiduoduoCityQuotaDto zhiduoduoCityQuota = reqdata.getZhiduoduoCityQuota();
		if (zhiduoduoCityQuota != null) {
			zhiduoduoCityQuotaMapper.insertOrUpdateByConfigId(zhiduoduoCityQuota.toEntity());
		}
		return SmartResult.success();
	}

	public SmartResult<MakerQuotaDto> getMakerQuota(BaseSimpleFormBean<Integer> formBean) {
		MakerQuotaDto makerQuotaDto = makerQuotaMapper.getByUserid(formBean.getReqdata());
		return SmartResult.success(makerQuotaDto);
	}

	public SmartResult editMakerQuota(BaseSimpleFormBean<MakerQuotaFormBean> formBean) {
		MakerQuotaFormBean reqdata = formBean.getReqdata();
		int userid = reqdata.getUserid();
		int quotaFlag = reqdata.getQuotaFlag();
		int twelveMonthQuotaFlag = reqdata.getTwelveMonthQuotaFlag();
		new MakerStudio().setUserid(userid).setQuotaFlag(quotaFlag).setTwelveMonthQuotaFlag(twelveMonthQuotaFlag).updateById();
		MakerQuota makerQuota = reqdata.toEntity();
		makerQuota.insertOrUpdate();
		return SmartResult.success();
	}

	public SmartResult<CompanyQuotaDto> getCompanyQuota(BaseSimpleFormBean<Integer> formBean) {
		CompanyQuotaDto companyQuotaDto = companyQuotaMapper.getByCompid(formBean.getReqdata());
		return SmartResult.success(companyQuotaDto);
	}

	public SmartResult editCompanyQuota(BaseSimpleFormBean<CompanyQuotaFormBean> formBean) {
		CompanyQuotaFormBean reqdata = formBean.getReqdata();
		int compid = reqdata.getCompid();
		int quotaFlag = reqdata.getQuotaFlag();
		new Company().setCompid(compid).setQuotaFlag(quotaFlag).updateById();
		CompanyQuota companyQuota = reqdata.toEntity();
		companyQuota.insertOrUpdate();
		return SmartResult.success();
	}
}
